<?php
/**
 * Pertyler
 * Row Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require 'main.php';

if((isset($page->ParentTable)) and (isset($page->Row)))
{
	$table = Pertyler_Table::Select($page->ParentTable);

	$row = $table->SelectRow($page->Row);

	$tree = $page->Tree($row);

	if(!$row)
		die('Row cannot found');

	$tree->AddLink('ObjectBrowser.php', 'leftMenu');

	if($table->FileTable())
	{
		$columnArray = $row->SelectColumnArray();

		if($table->TableType() == 'imageFile')
			$file = Image::Select($fileFolder . $table->uniqueName() . '/', $columnArray[1]->Value(), $columnArray[3]->Value());
		else
			$file = File::Select($fileFolder . $table->uniqueName() . '/', $columnArray[1]->Value(), $columnArray[3]->Value());

		if(!$file)
			die('File cannot found');

		$tree->AddLink($file->Address());
	}

	if($tree->Pressed() == 'change_okay')
	{
		foreach($row->SelectColumnArray() AS $column)
			switch($column->ColumnType())
			{
				case 'dropdown':
					if($_POST['column' . $column->Priority()])
						$inputColumnArray[$column->Priority()] = $column->RelatedTable()->SelectRow($_POST['column' . $column->Priority()]);
					else
						$inputColumnArray['column' . $column->Priority()] = NULL;

					break;
				case 'integer':
					if(($table->FileTable()) AND ($column->Priority() == 4))
						$inputColumnArray[4] = $file->Size();
					else
						if($_POST['column' . $column->Priority()])
							$inputColumnArray[$column->Priority()] = (int) $_POST['column' . $column->Priority()];
						else
							$inputColumnArray[$column->Priority()] = NULL;

					break;
				case 'string':
					if(($table->FileTable()) AND ($column->Priority() == 3))
						$inputColumnArray[3] = $file->Extension();
					else
						if($_POST['column' . $column->Priority()])
							$inputColumnArray[$column->Priority()] = $_POST['column' . $column->Priority()];
						else
							$inputColumnArray[$column->Priority()] = NULL;

					break;
				case 'text':
					if($_POST['column' . $column->Priority()])
						$inputColumnArray[$column->Priority()] = $_POST['column' . $column->Priority()];
					else
						$inputColumnArray[$column->Priority()] = NULL;

					break;
				case 'paragraph':
					if($_POST['column' . $column->Priority()])
						$inputColumnArray[$column->Priority()] = $_POST['column' . $column->Priority()];
					else
						$inputColumnArray[$column->Priority()] = NULL;

					break;
				case 'date':
					if($_POST['column' . $column->Priority()])
						$inputColumnArray[$column->Priority()] = Date::Select($_POST['column' . $column->Priority()]);
					else
						$inputColumnArray[$column->Priority()] = NULL;

					break;
				case 'boolean':
					if(($column->Priority() == 2) AND ($column->backgroundName() == 'Alternative'))
						$inputColumnArray[2] = $column->Value();
					else
						if(isset($_POST['column' . $column->Priority()]))
							$inputColumnArray[$column->Priority()] = true;
						else
							$inputColumnArray[$column->Priority()] = false;

					break;
				case 'float':
					$inputColumnArray[$column->Priority()] = (float) $_POST['column' . $column->Priority()];

					break;
			}

		try
		{
			if($row->Update($inputColumnArray))
			{
				if($table->FileTable())
					$file->Rename($_POST['column1']);

				$tree->AddProperty('Updated
<script type="text/javascript">
parent.leftMenu.document.location.reload();
</script>');
			}
		}
		catch(Exception $e)
		{
			$tree->AddProperty('Update failed');
		}
	}
	else if($tree->Pressed() == 'delete_okay')
	{
		try
		{
			$row->Delete();

			foreach($row->SelectColumnArray() AS $column)
			{
				if($column->ColumnType() == 'boolean')
					if($column->Value())
						$leaf = $tree->Add(new TreeLeaf($column, $column));
					else
						$leaf = $tree->Add(new TreeLeaf($column));
				else
					$leaf = $tree->Add(new TreeLeaf($column, $column->Value()));

				if(($column->Value()) AND ($column->RelatedTable()))
				{
					$leaf->AddParameter('ParentTable', $column->RelatedTable()->id());

					$leaf->AddParameter('Row', $column->Value()->id());

					$leaf->AddLink('ObjectBrowser.php');
				}
			}

			$tree->AddProperty('Deleted
<script type="text/javascript">
parent.leftMenu.document.location.reload();
</script>');

			unset($row);
		}
		catch(Exception $e)
		{
			$tree->AddProperty('Cannot deleted');
		}
	}
	else if($tree->Pressed() == 'approve')
		$row->UpdateAsApproved();

	if($tree->Pressed() == 'change')
	{
		$tree->AddButton('change_okay');
		$tree->AddButton('cancel');

		$leaf = $tree->Add(new TreeLeaf('Last Changed Time', new Date));

		if($table->SelectPrivilege('approve'))
			$leaf->Colour('#006666');

		$leaf = $tree->Add(new TreeLeaf('Last Changed Moderator', Pertyler_Role_Moderator::Select()));

		foreach($row->SelectColumnArray() AS $column)
			switch($column->ColumnType())
			{
				case 'dropdown':
					$html = '
<select name="column' . $column->Priority() . '" >
<option value="" >
</option>';

					foreach($column->RelatedTable()->SelectRowArray() AS $row)
					{
						$html .= '
<option value="' . $row->id() . '" ';

						if($column->Value() == $row)
							$html .= 'selected ';

						$html .= '>
			' . $row . '
</option>';
					}

					$html .= '
</select>';

					$tree->Add(new TreeLeaf($column, $html));

					break;
				case 'integer':
					if(($table->FileTable()) AND ($column->Priority() == 4))
						$tree->Add(new TreeLeaf($column, $file->Size()));
					else 
						$tree->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" value="' . $column->Value() . '" maxlength="6" />'));

					break;
				case 'string':
					if(($table->FileTable()) AND ($column->Priority() == 3))
						$tree->Add(new TreeLeaf($column, $file->Extension()));
					else
						$tree->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" value="' . $column->Value() . '" maxlength="60" />'));

					break;
				case 'text':
					$html = <<<EOF
<input type="button" value="Bold" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'b');" >
<input type="button" value="Italic" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'i');" >
<input type="button" value="Paragraph" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'p');" >
<input type="button" value="&lt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;lt;');" >
<input type="button" value="&gt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;gt;');" >
<input type="button" value="&amp;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;amp;');" >
<input type="button" value="&quot;" onclick="InsertTwoCharacter('textArea{$column->Priority()}', '&amp;quot;');" >
EOF;

					$tree->Add(new TreeLeaf($html));

					$tree->Add(new TreeLeaf($column, '<textarea rows="20" cols="50%" id="textArea' . $column->Priority() . '" name="column' . $column->Priority() . '" >' . str_replace('&', '&amp;', $column->Value()) . '</textarea>'));

					break;
				case 'paragraph':
					$html = <<<EOF
<input type="button" value="Bold" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'b');" >
<input type="button" value="Italic" onclick="InsertHTMLTag('textArea{$column->Priority()}', 'i');" >
<input type="button" value="&lt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;lt;');" >
<input type="button" value="&gt;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;gt;');" >
<input type="button" value="&amp;" onclick="InsertCharacter('textArea{$column->Priority()}', '&amp;amp;');" >
<input type="button" value="&quot;" onclick="InsertTwoCharacter('textArea{$column->Priority()}', '&amp;quot;');" >
EOF;

					$tree->Add(new TreeLeaf($html));

					$tree->Add(new TreeLeaf($column, '<textarea rows="20" cols="50%" id="textArea' . $column->Priority() . '" name="column' . $column->Priority() . '" >' . str_replace('&', '&amp;', $column->Value()) . '</textarea>'));

					break;
				case 'date':
					$tree->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" value="' . $column->Value() . '" maxlength="11" />'));

					break;
				case 'boolean':
					if(($column->Priority() == 2) AND ($column->backgroundName() == 'Alternative'))
						$tree->Add(new TreeLeaf($column, $column->Value()));
					else
					{
						$html = '<input type="checkbox" name="column' . $column->Priority() . '" ';

						if($column->Value())
							$html .= 'checked ';

						$html .= '/>';

						$tree->Add(new TreeLeaf($column, $html));
					}

					break;
				case 'float':
					$tree->Add(new TreeLeaf($column, '<input type="text" name="column' . $column->Priority() . '" value="' . $column->Value() . '" maxlength="12" />'));

					break;
				default:
					$tree->Add(new TreeLeaf($column, $column->Value()));
			}
	}
	else if(isset($row))
	{
		if($tree->Pressed() == 'delete')
		{
			$tree->AddButton('delete_okay');

			$tree->AddButton('cancel');
		}
		else if(!$row->VirtualRow())
		{
			if(($table->SelectPrivilege('update')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('update'))))
				$tree->AddButton('change');

			if(($table->SelectPrivilege('delete')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('delete'))))
				$tree->AddButton('delete');

			if($table->SelectPrivilege('approve'))
			{
				if((Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('approve'))) AND (!$row->SelectApproved()))
					$tree->AddButton('approve');
			
				if($row->SelectLastChangeLogTime())
				{
					$timeLeaf = $tree->Add(new TreeLeaf('Last Changed Time', $row->SelectLastChangeLogTime()));
					
					if(($table->SelectPrivilege('approve')) AND (!$row->SelectApproved()))
						$timeLeaf->Colour('#006666');

					$moderatorLeaf = $tree->Add(new TreeLeaf('Last Changed Moderator', $row->SelectLastChangeLogModeratorName()));
				}
			}
		}

		foreach($row->SelectColumnArray() AS $column)
		{
			if($column->ColumnType() == 'boolean')
				if($column->Value())
					$leaf = $tree->Add(new TreeLeaf($column, $column));
				else
					$leaf = $tree->Add(new TreeLeaf($column));
			else
				$leaf = $tree->Add(new TreeLeaf($column, $column->Value()));

			if(($column->Value()) AND ($column->RelatedTable()))
			{
				$leaf->AddParameter('ParentTable', $column->RelatedTable()->id());

				$leaf->AddParameter('Row', $column->Value()->id());

				$leaf->AddLink('ObjectBrowser.php');
			}
		}
	}

	echo $tree;
}